var express = require("express");
var app = express();
var config = require("./app/conf/config.json");
var authRouter = require("./app/router/auth");
var versionRouter = require("./app/router/version");
var rechargeRouter = require("./app/router/recharge");
var bodyParser = require('body-parser');

app.use(bodyParser.json());
var log4js = require('log4js');

log4js.configure({
    "appenders": {
        "console": {
            "type": "console"
        },
        "trace": {
            "type": "file",
            "filename": "log/access.log",
            "maxLogSize ": 31457280
        },
        "http": {
            "type": "logLevelFilter",
            "appender": "trace",
            "level": "trace",
            "maxLevel": "trace"
        },
        "info": {
            "type": "dateFile",
            "filename": "log/app-info.log",
            "pattern": ".yyyy-MM-dd",
            "layout": {
                "type": "pattern",
                "pattern": "[%d{ISO8601}][%5p  %z  %c] %m"
            },
            "compress": true
        },
        "maxInfo": {
            "type": "logLevelFilter",
            "appender": "info",
            "level": "debug",
            "maxLevel": "info"
        },
        "error": {
            "type": "dateFile",
            "filename": "log/app-error.log",
            "pattern": ".yyyy-MM-dd",
            "layout": {
                "type": "pattern",
                "pattern": "[%d{ISO8601}][%5p  %z  %c] %m"
            },
            "compress": true
        },
        "minError": {
            "type": "logLevelFilter",
            "appender": "error",
            "level": "error"
        }
    },
    "categories": {
        "default": {
            "appenders": [
                "console",
                "http",
                "maxInfo",
                "minError"
            ],
            "level": "all"
        }
    }
});

app.use("/auth", authRouter);
app.use("/version", versionRouter);
app.use("/recharge", rechargeRouter);
app.listen(config['port'], "0.0.0.0"); // ipv4
console.log("listen to %s", config['port']);